package com.chcplayer.ui
{
import com.somerandomdude.colortoolkit.spaces.HSB;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

/**
 * author yh
 */
public class HsbColorChooser extends Sprite
{
	/**
	 */
	public static const HUE_X:int = 380;
	/**
	 */
	public static const HUE_W:int = 28;
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	/**
	 */
	private var m_bmdSB:BitmapData = new BitmapData(100, 100, false);
	private var m_picSB:Bitmap = new Bitmap();
	private var m_contSB:Sprite = new Sprite();
	/**
	 */
	private var m_bmdHue:BitmapData = new BitmapData(HUE_W, 360, false);
	private var m_picHue:Bitmap = new Bitmap();
	private var m_contHue:Sprite = new Sprite();
	/**
	 */
	private var m_obAdjuster:ColorHueAdjuster = new ColorHueAdjuster();
	/**
	 */
	[Embed(source="Assets/Icon/ui/ColorRing.png")]
	private var m_cColorRing:Class;
	private var m_picRing:Bitmap;
	/**
	 */
	private var m_obRing:Sprite = new Sprite();
	/**
	 */
	private var m_obHsb:HSB = new HSB();
	/**
	 */
	public function HsbColorChooser()
	{
		super();
		this._init();
	}
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	/**
	 */
	private function _updateSB():void
	{
		var hsb:HSB = new HSB(m_obHsb.hue);
		for (var i:int = 0; i < 100; i++)
		{
			hsb.brightness = 100 - i;
			for (var j:int = 0; j < 100; j++)
			{
				hsb.saturation = j;
				m_bmdSB.setPixel(j, i, hsb.color);
			}
		}
	} 
	/**
	 */
	private function _init():void
	{
		m_contHue.x = HUE_X;
		m_contHue.addChild(m_picHue);
		m_obAdjuster.setChooser(this);
		m_contHue.addChild(m_obAdjuster);
		m_contHue.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_hue);
		m_picRing = new m_cColorRing();
		m_contSB.scrollRect = new Rectangle(0, 0, 360, 360);
		m_contSB.addChild(m_picSB);
		/*
		*/
		m_picRing.x = -int(m_picRing.width>>1);
		m_picRing.y = -int(m_picRing.height>>1);
		m_obRing.addChild(m_picRing);
		m_contSB.addChild(m_obRing);
		m_contSB.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_sb);
		/*
		*/
		m_picHue.bitmapData = m_bmdHue;
		var hsb:HSB = new HSB(0,100,100);
		var rectL:Rectangle = new Rectangle(0, 0, HUE_W, 1);
		for (var i:int = 0; i < 360; i++) 
		{
			hsb.hue = i;
			rectL.y = i;
			m_bmdHue.fillRect(rectL, hsb.color);
		}
		this._updateSB();
		/*
		*/
		m_picSB.bitmapData = m_bmdSB;
		m_picSB.width = 360;
		m_picSB.height = 360;
		/*
		*/
		this.addChild(m_contSB);
		this.addChild(m_contHue);
	}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// events
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	private function mouseDown_sb(e:MouseEvent):void
	{
		m_obHsb.saturation = m_picSB.mouseX;
		m_obHsb.brightness = 100 - m_picSB.mouseY;
		m_obRing.startDrag(true, new Rectangle(0, 0, 360, 360) );
		/*
		*/
		this.dispatchEvent(new Event(Event.CHANGE));
		this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stage);
		this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp_stage);
	}
	/**
	 */
	private function mouseDown_hue(e:MouseEvent):void
	{
		m_obHsb.hue = m_picHue.mouseY;
		this._updateSB();
		this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stageHue);
		this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp_stageHue);
		/*
		*/
		this.dispatchEvent(new Event(Event.CHANGE) );
	}
	/**
	 */
	private function mouseMove_stageHue(e:MouseEvent):void
	{
		m_obHsb.hue = m_picHue.mouseY;
		this._updateSB();
		this.dispatchEvent(new Event(Event.CHANGE) );
	}
	/**
	 */
	private function mouseUp_stageHue(e:MouseEvent):void
	{
		this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stageHue);
		this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_stageHue);
	}
	/**
	 * 修改亮度$饱和度
	 */
	private function mouseMove_stage(e:MouseEvent):void
	{
		m_obHsb.saturation = m_picSB.mouseX;
		m_obHsb.brightness = 100 - m_picSB.mouseY;
		this.dispatchEvent(new Event(Event.CHANGE) );
	}
	/**
	 */
	private function mouseUp_stage(e:MouseEvent):void
	{
		this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stage);
		this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_stage);
		m_obRing.stopDrag();
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function setColor(nC:int):void
	{
		m_obHsb.color = nC;
		if ( isNaN(m_obHsb.hue) )
		{
			m_obHsb.hue = 0;
		}
		m_obAdjuster.y = m_obHsb.hue;
		m_obRing.x = m_obHsb.saturation * 3.6;
		m_obRing.y = 3.6 * (100 - m_obHsb.brightness);
		this._updateSB();
		this.dispatchEvent(new Event(Event.CHANGE) );
	}
	/**
	 */
	public function getColor():int
	{
		return m_obHsb.color;
	}
	/**
	 */
	public function getHueStripWidth():int
	{
		return HUE_W;
	}
	/**
	 */
	public function getHue():int
	{
		return m_obHsb.hue;
	}
	/**
	 */
}
}